package com.cpi.cornpp.creditline.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.creditline.exception.CreditLineException;
import com.cpi.cornpp.creditline.form.CreditLineForm;
import com.cpi.cornpp.creditline.services.CreditLineService;

/**
 * Credit Line Add Action
 * @author Edgar Joao
 *
 */
public class CreditLineAddAction extends Action{

	/** Error message name*/
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Fail forward */
	private final static String FAIL_FORWARD = "fail";//$NON-NLS-1$
	/** Success forward */
	private final static String SUCCESS_FORWARD = "success";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Success add key */
	private static final String SUCCESS_ADD_LABEL = "common.add.success";//$NON-NLS-1$
	/** Success edit key */
	private static final String SUCCESS_EDIT_LABEL = "common.edit.success";//$NON-NLS-1$
	

	/**
	 * Add a Credit Line
	 * @author Edgar Joao
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		CreditLineService creditLineService = null;
		MessageResources messageResources = null;
		String messageError = null;				
		CreditLineForm creditLineForm = null;
		String forward = null;
		HttpSession httpSession = null;		
		boolean result = false;
		
		try{			
			
			httpSession = request.getSession(false);
			messageResources = getResources(request);
			creditLineForm = (CreditLineForm) form;
		
		if(creditLineForm != null){
			creditLineService = new CreditLineService();
			result = creditLineService.existCreditLine();  
			
			if(result){
				creditLineService.updateCreditLine(creditLineForm.getCreditLineView());
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_EDIT_LABEL);			
				httpSession.setAttribute(MESSAGE, messageError);			
				forward = SUCCESS_FORWARD;				
			} else if(!result){					
				creditLineService.addCreditLine(creditLineForm.getCreditLineView());
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_ADD_LABEL);				
				httpSession.setAttribute(MESSAGE, messageError);			
				forward = SUCCESS_FORWARD;				
			}else {				
				httpSession = request.getSession(false);
				messageResources = getResources(request);
				form.reset(mapping, request);
				forward = FAIL_FORWARD;
			}			
			
		}
		
		}catch (CreditLineException creditLineException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), creditLineException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, creditLineException);
				form.reset(mapping, request);
				throw creditLineException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
				forward = FAIL_FORWARD;
			}
			throw creditLineException;
		}
		catch (Exception e){
			CreditLineException creditLineException=null;
			e.printStackTrace(System.out);
			creditLineException= new CreditLineException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_INSERT);
			throw creditLineException;
		}
		form.reset(mapping, request);			
		return mapping.findForward(forward);
	}
}
